package v1

import (
	"hr/api/dto"
	"hr/api/middleware"
	"hr/db"
	"hr/utils"

	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
)

// GetExpenseDeclarationApproval
// @Summary 获取所有费用申报
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param pageNo query string true "页码"
// @Param pageSize query string true "页大小"
// @Param type query string false "类别 1 小队长待审核"
// @Param name query string false "名称"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval [get]
func GetExpenseDeclarationApproval(c *gin.Context) {
	pageNoStr := c.Query("pageNo")
	pageSizeStr := c.Query("pageSize")
	name := c.Query("name")
	from := c.Query("type")

	var pageNo, pageSize int
	if !utils.IsStringEmpty(pageNoStr) {
		pageNo = cast.ToInt(pageNoStr)
	}
	if !utils.IsStringEmpty(pageSizeStr) {
		pageSize = cast.ToInt(pageSizeStr)
	}
	paging := &dto.Pagination{
		PageNo:   pageNo,
		PageSize: pageSize,
	}
	id, _ := middleware.GetCurrentUserId(c)
	d, err := db.GetExpenseDeclarationApprovalPaging(*paging, name, id, from)
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}
	utils.EchoSuccess(c, d, "请求成功")
	return
}

// CreateExpenseDeclarationApproval
// @Summary 创建费用申报类型
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param ExpenseDeclarationApproval body db.ExpenseDeclarationApproval true "信息"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval [post]
func CreateExpenseDeclarationApproval(c *gin.Context) {
	p := &db.ExpenseDeclarationApproval{}
	if err := c.ShouldBindJSON(p); err != nil {
		utils.EchoError(c, "缺少必要参数信息")
		return
	}
	id, _ := middleware.GetCurrentUserId(c)
	m, err := db.CreateExpenseDeclarationApproval(*p, cast.ToString(id))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, m, "创建成功")
	return
}

// UpdateExpenseDeclarationApproval
// @Summary 更新费用申报
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param ExpenseDeclarationApproval body db.ExpenseDeclarationApproval true "信息"
// @Param id path string true "主键ID"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval/{id} [put]
func UpdateExpenseDeclarationApproval(c *gin.Context) {
	p := &db.ExpenseDeclarationApproval{}
	if err := c.ShouldBindJSON(p); err != nil {
		utils.EchoError(c, "缺少必要参数信息")
		return
	}
	idStr := c.Param("id")
	if utils.IsStringEmpty(idStr) {
		utils.EchoError(c, "缺少ID")
		return
	}
	_, _, tag := db.GetExpenseDeclarationApproval(map[string]interface{}{"id": idStr})
	if tag == "0" || tag == "1" {
		utils.EchoError(c, "此费用申报不存在")
		return
	}
	id, _ := middleware.GetCurrentUserId(c)
	m, err := db.UpdateExpenseDeclarationApproval(cast.ToUint(idStr), *p, cast.ToString(id))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, m, "更新成功")
	return
}

// DeleteExpenseDeclarationApproval
// @Summary 删除费用申报
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param id path string true "主键ID"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval/{id} [delete]
func DeleteExpenseDeclarationApproval(c *gin.Context) {
	idStr := c.Param("id")
	if utils.IsStringEmpty(idStr) {
		utils.EchoError(c, "缺少ID")
		return
	}
	_, _, tag := db.GetExpenseDeclarationApproval(map[string]interface{}{"id": idStr})
	if tag == "0" || tag == "1" {
		utils.EchoError(c, "此费用申报不存在")
		return
	}
	_, err := db.DeleteExpenseDeclarationApproval(cast.ToUint(idStr))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, nil, "删除成功")
	return
}

// UpdateExpenseDeclarationApprovalOrderStatus
// @Summary 修改费用申报状态
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param order_status path string true "申请状态"
// @Param id path string true "主键ID"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval/{id}/{order_status} [put]
func UpdateExpenseDeclarationApprovalOrderStatus(c *gin.Context) {
	status := c.Param("order_status")
	if utils.IsStringEmpty(status) {
		utils.EchoError(c, "状态不能为空")
		return
	}
	idStr := c.Param("id")
	if utils.IsStringEmpty(idStr) {
		utils.EchoError(c, "缺少ID")
		return
	}
	_, _, tag := db.GetExpenseDeclarationApproval(map[string]interface{}{"id": idStr})
	if tag == "0" || tag == "1" {
		utils.EchoError(c, "此费用申报不存在")
		return
	}
	id, _ := middleware.GetCurrentUserId(c)
	m, err := db.UpdateExpenseDeclarationApprovalOrderStatus(cast.ToUint(idStr), status, id)
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, m, "更新成功")
	return
}

// UpdateExpenseDeclarationApprovalApprovalStatus
// @Summary 修改费用申报审批状态
// @Tags 费用申报相关api
// @Accept  json
// @Produce json
// @Param Access-Token header string true "Authentication header"
// @Param ApprovalRequest body dto.ApprovalRequest true "信息"
// @Param id path string true "主键ID"
// @Success 200 {object} utils.JsonContent
// @Failure 400 {object} utils.JsonContent
// @Failure 500 {object} utils.JsonContent
// @Router /api/v1/auth/expense_declaration_approval/{id} [put]
func UpdateExpenseDeclarationApprovalApprovalStatus(c *gin.Context) {
	p := &dto.ApprovalRequest{}
	if err := c.ShouldBindJSON(p); err != nil {
		utils.EchoError(c, "缺少必要参数信息")
		return
	}
	idStr := c.Param("id")
	if utils.IsStringEmpty(idStr) {
		utils.EchoError(c, "缺少ID")
		return
	}
	_, _, tag := db.GetExpenseDeclarationApproval(map[string]interface{}{"id": idStr})
	if tag == "0" || tag == "1" {
		utils.EchoError(c, "此费用申报不存在")
		return
	}
	m, err := db.UpdateExpenseDeclarationApprovalApprovalStatus(*p, cast.ToUint(idStr))
	if err != nil {
		utils.EchoError(c, err.Error())
		return
	}

	utils.EchoSuccess(c, m, "更新成功")
	return
}
